﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using AutoMapper.Mappers;
using GurLoader.GlobalHotkey;
using Moq;
using NUnit.Framework;

namespace GurLoaderWPF.Test.GlobalHotKey
{
    [TestFixture]
    public class HotKeyAlreadyRegisteredExceptionTests
    {
        [Test]
        public void CreateInstanceStringHotkeyTest()
        {
            string message = "Test Error Message";
            HotKey hk = new HotKey(Key.C, ModifierKeys.Alt, true);

            HotKeyAlreadyRegisteredException exception = new HotKeyAlreadyRegisteredException(message, hk);

            Assert.IsNotNull(exception);
            Assert.AreEqual(message, exception.Message);
            Assert.AreEqual(hk, exception.HotKey);
        }

        [Test]
        public void CreateInstanceStringHotkeyExceptionTest()
        {
            string message = "Test Error Message";
            HotKey hk = new HotKey(Key.C, ModifierKeys.Alt, true);
            Exception parentException = new Exception("parentException");

            HotKeyAlreadyRegisteredException exception = new HotKeyAlreadyRegisteredException(message, hk, parentException);

            Assert.IsNotNull(exception);
            Assert.AreEqual(message, exception.Message);
            Assert.AreEqual(hk, exception.HotKey);
            Assert.AreEqual(parentException, exception.InnerException);
        }

        [Test]
        public void CreateInstanceFromSerializationTest()
        {
            string message = "Test Error Message";
            HotKey hk = new HotKey(Key.C, ModifierKeys.Alt, true);

            HotKeyAlreadyRegisteredException exception = new HotKeyAlreadyRegisteredException(message, hk);
            
            HotKeyAlreadyRegisteredException afterDeserializationException;

            using (Stream s = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(s, exception);
                s.Position = 0; // Reset stream position
                afterDeserializationException = (HotKeyAlreadyRegisteredException)formatter.Deserialize(s);
            }

            Assert.IsNotNull(afterDeserializationException);
            Assert.AreEqual(message, afterDeserializationException.Message);
            Assert.AreEqual(hk, afterDeserializationException.HotKey);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetObjectDataNullInfoTest()
        {
            SerializationInfo info = null;
            StreamingContext sc = new StreamingContext();

            string message = "Test Error Message";
            HotKey hk = new HotKey(Key.C, ModifierKeys.Alt, true);

            HotKeyAlreadyRegisteredException exception = new HotKeyAlreadyRegisteredException(message, hk);

            exception.GetObjectData(info, sc);
        }

    }
}
